from pyomo.environ import *

model = ConcreteModel()

#定义决策变量
model.x1 = Var(domain=Reals)
model.x2 = Var(domain=Reals)
model.x3 = Var(domain=Reals)
model.x4 = Var(domain=Reals)

#定义目标函数
model.z = Objective(expr = 2*model.x1 + 4*model.x2 + 2*model.x3+4*model.x4,sense=minimize)

#定义约束条件
model.demand1_1 = Constraint(expr = model.x1<=1)
model.demand1_2 = Constraint(expr = model.x1>=0)
model.demand2_1 = Constraint(expr = model.x2<=1)
model.demand2_2 = Constraint(expr = model.x2>=0)
model.demand3_1 = Constraint(expr = model.x3<=1)
model.demand3_2 = Constraint(expr = model.x3>=0)
model.demand4_1 = Constraint(expr = model.x4<=1)
model.demand4_2 = Constraint(expr = model.x4>=0)
model.subject1 = Constraint(expr = model.x1+model.x2>=1)
model.subject2 = Constraint(expr = model.x2+model.x3>=1)
model.subject3 = Constraint(expr = model.x3+model.x4>=1)
model.subject4 = Constraint(expr = model.x1+model.x4>=1)

#打印模型的相关信息
model.pprint()

#求解最优解
opt = SolverFactory('glpk', executable='C:\\software\\winglpk-4.65\\glpk-4.65\\w64\\glpsol')
opt.solve(model)

print("\noptimal solution:\n")
print("x1={}\n".format(model.x1()))
print("x2={}\n".format(model.x2()))
print("x3={}\n".format(model.x3()))
print("x4={}\n".format(model.x4()))
print("z={}\n".format(model.z()))